package com.example.rpAdapter.web;


import com.example.common.annotation.ResponseResult;
import com.example.config.enums.LdExceptionEnum;
import com.example.config.exception.RLdCodeException;
import com.example.config.util.AssertUtil;
import com.example.rpapp.activity.command.RedisDeductionAwardNumberDrawExe;
import com.example.rpapp.context.ActivityDrawContext;
import com.example.rpapp.listener.AwardInventoryToRedisApplicationListener;
import com.example.rpapp.listener.event.ActivityCreateEvent;
import com.example.rpapp.mq.producer.ActivityDrawMessageProducer;
import com.example.rpclient.api.IRecordServer;
import com.example.rpclient.dto.data.ActivityConfigVo;
import com.example.rpclient.dto.data.ActivityVo;
import com.example.rpclient.dto.data.AwardVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@AllArgsConstructor
@ResponseResult
@RequestMapping("/v1/test")
public class TestController {

    private final ApplicationEventMulticaster applicationEventMulticaster;

    private final RedisDeductionAwardNumberDrawExe drawExe;

    private final ActivityDrawMessageProducer activityDrawMessageProducer;
//
    private final IRecordServer recordServer;

    @GetMapping("/errorTest01")
    public void errorTest01() {
        throw new RLdCodeException(5050, "测试错误！");
    }

    @GetMapping("/errorTest02")
    public void errorTest02() {
        AssertUtil.isTrue(Boolean.TRUE, LdExceptionEnum.ADD_ERROR.getDescription());
    }

    @GetMapping("/activityCreateEventTest")
    public void activityCreateEventTest() {

        ActivityConfigVo activityConfigVO = new ActivityConfigVo();

        ActivityVo activityVo = new ActivityVo();
        activityVo.setId(1L);
        activityConfigVO.setActivityVo(activityVo);


        List<AwardVo> awardVOList = new ArrayList<>();
        AwardVo awardVO = new AwardVo();
        awardVO.setAwardName("测试奖项");
        awardVO.setId(100L);
        awardVO.setPrizeId(1L);
        awardVO.setNumber(200);
        awardVOList.add(awardVO);


        AwardVo awardVO2 = new AwardVo();
        awardVO2.setAwardName("测试奖项");
        awardVO2.setId(200L);
        awardVO2.setPrizeId(1L);
        awardVO2.setNumber(400);
        awardVOList.add(awardVO2);
        activityConfigVO.setAwardVoList(awardVOList);
        // 发送活动创建事件
        applicationEventMulticaster.multicastEvent(new ActivityCreateEvent("", activityConfigVO));
    }


    @GetMapping("/invokeStockDeductionLuaTest")
    public Integer invokeStockDeductionLuaTest() {
        return drawExe.invokeStockDeductionLua(
                AwardInventoryToRedisApplicationListener.getKey(1L, 100L));
    }

    @GetMapping("/invokeStockRollbackLuaTest")
    public Integer invokeStockRollbackLuaTest() {
        return drawExe.invokeStockRollbackLua(
                AwardInventoryToRedisApplicationListener.getKey(1L, 100L));
    }

    @GetMapping("/activityDrawMessageProducerTest")
    public Boolean activityDrawMessageProducerTest() {
        return activityDrawMessageProducer.sendTest(new ActivityDrawContext());
    }

    @GetMapping("/exchangeMoneyTest")
    public Boolean exchangeMoneyTest(@RequestParam("recordId") Long recordId) {
        return recordServer.exchangeMoney(recordId);
    }
}
